﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MMITPinGen.Entities;
using MMITCore.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Web.Security;
using MMITCore;
using MMITDomain.Entities;
using MMITDomain;
using MMITDomain.Concrete;

namespace MMITPinGen
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        EFPartnerRepository PartnerRepository = new EFPartnerRepository();
        EFPINRepository PINRepository = new EFPINRepository();
        EFBatchRepository BatchRepository = new EFBatchRepository();
        PIN pin;
        Batch b;
        SynkronLogger Log = new SynkronLogger("MMITPINGen");
        Dictionary<string, object> d = new Dictionary<string, object>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<string> roles = Roles.GetRolesForUser(User.Identity.Name).ToList();
                var r = from p in roles
                        where p == "MMIT Admin"
                        select p;
                if (r.Count() == 0)
                {
                    lblMsg.Text = "You do not have permission to perform this function. Please contact Customer Service";
                    tblForm.Visible = false;
                }
                drpPartners.Items.Add(new Telerik.Web.UI.RadComboBoxItem { Text = "Select Partner", Value = "0" });
                drpPartners.AppendDataBoundItems = true;
                List<Partner> lp = (from p in PartnerRepository.Partners
                                    select p).ToList();
                drpPartners.DataSource = lp;
                drpPartners.DataBind();


            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            PIN Pseed = PINRepository.GetSeedPIN();
            long seed = 0;
            if (Pseed != null)
            { seed = Pseed.Id; }

            MMITCore.TransactionActions.OTP otp = new MMITCore.TransactionActions.OTP();
            int SerialLength = Convert.ToInt32(drpSerialLength.SelectedValue);
            int PINLength = Convert.ToInt32(drpPINLength.SelectedValue);
            int status = Convert.ToInt32(this.drpStatus.SelectedValue);
            b = new Batch { GeneratedBy = User.Identity.Name, DateGenerated = DateTime.Now, Name = txtName.Text, PartnerId = Convert.ToInt32(drpPartners.SelectedValue), Quantity = Convert.ToInt32(drpQuantity.SelectedValue), Status = Convert.ToInt32(drpStatus.SelectedValue), Value = Convert.ToDecimal(txtValue.Text) };
            BatchRepository.SaveBatch(b);
            for (int i = 1; i <= Convert.ToInt32(drpQuantity.SelectedValue); i++)
            {
                pin = new PIN { PINNumber = otp.Generate(PINLength).ToString(), SerialNo = txtPrefix.Text + (i + seed).ToString().PadLeft(SerialLength - txtPrefix.Text.Length, '0'), DateGenerated = DateTime.Now, Value = Convert.ToDecimal(txtValue.Text), BatchId = b.Id, Status = status };
                PINRepository.SavePIN(pin);

            }

            d.Clear();
            d.Add("Batch Created", Log.Desc(b));
            Log.WriteInformation(23234030, "", d);
            tblForm.Visible = false;
            lblMsg.Text = "PIN Batch Successfully Generated";
            lnkNew.Visible = true;

        }

        private void ClearControls()
        {
            this.drpPartners.SelectedIndex = 0;
            this.drpPINLength.SelectedIndex = 0;
            this.drpQuantity.SelectedIndex = 0;
            this.drpSerialLength.SelectedIndex = 0;
            this.drpStatus.SelectedIndex = 0;
            this.txtPrefix.Text = string.Empty;
            this.txtName.Text = string.Empty;
            this.txtValue.Text = string.Empty;
            lblMsg.Text = string.Empty;
            lnkNew.Visible = false;
            tblForm.Visible = true;



        }

        protected void lnkNew_Click(object sender, EventArgs e)
        {
            ClearControls();
        }
    }
}